<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/3
 * Time: 12:57
 */

namespace app\admin\controller;


use app\model\CategoryModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Category
{
    public function index()
    {
        // 查找分类数据
        $categoryList = CategoryModel::select();

        View::assign('categoryList', $categoryList);
        return View::fetch();
    }
    public function add()
    {
        return View::fetch();
    }
    public function addSave()
    {
        // 获取参数
        // 校验参数
        $params = Request::param();

        $validate = Validate::rule([
            'category_name|分类名称' => 'require|min:2|max:45',
        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }

        if (CategoryModel::getByCategoryName($params['category_name'])) {
            echo '分类已经存在，请用其他分类名。';
            echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
            exit();
        }

        // 插入数据
        // 连接数据库（TP做好了）
        // 新建一个模型
        // 模型操作增加
        $params['add_time'] = time();
        $params['update_time'] = time();
        $result = CategoryModel::create($params);
        return View::fetch('public/tips', ['result' => $result]);

    }

    public function edit()
    {
        // 整出视图，
        // 查询出要编辑的分类信息
        // 获取参数category_id
        $categoryId = Request::param("category_id");
        $validate =Validate::rule([
            'category_id|ID' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['category_id' => $categoryId])) {
            echo $validate->getError();
            exit();
        }

        $category =CategoryModel::find($categoryId);
        if (!$category) {
            echo '分类不存在';
            exit();
        }

        return View::fetch('', ['category' => $category]);
    }
    // 保存数据
    public function editSave()
    {
        $params = Request::param(); // 获取参数
        $validate =Validate::rule([
            'category_id|分类id' => 'require|between:1,' . PHP_INT_MAX,
            'category_name|分类名称' => 'require|min:2|max:45',
        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }
        // update
        // 查找出对应的分类信息
        $category =CategoryModel::find($params['category_id']);
        if (!$category) {
            echo '分类不存在';
            exit();
        }
        $category['category_name'] = $params['category_name'];

        $category['update_time'] = time();
        $result = $category->save();
        return View::fetch('public/tips', ['result' => $result]);

    }
    public function delete()
    {
        $categoryId = Request::param("category_id");
        $validate = Validate::rule([
            'category_id|分类id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['category_id' => $categoryId])) {
            echo $validate->getError();
            exit();
        }


        $result = CategoryModel::destroy($categoryId);
        return View::fetch('public/tips', ['result' => $result]);

    }
}